﻿
var ns, toast, actionsheet;
var app = new Vue({
    el: '#app',
    data: {
        RoomID: 0,
        RoomIdList: [],
        allroomList: [],
        roomList: [],
        chargeList: [],
        form: {
            id: 0,
            roomId: 0,
            fullName: '阳光雅筑-住宅-1栋',
            chargeId: 0,
            useCount: '',
            remark: '',
            feeDate: '',
            feeTypeList: [
                { id: 1, name: '物业费/物业管理费1.5', nextTime: '2023-10-23', price: '1.56元', feeRound: '2个月', isSelect: false },
                { id: 2, name: '物业费1.58', nextTime: '2023-11-08', price: '2.10元', feeRound: '12个月', isSelect: false },
            ],
        },
        IsMaskOpen: false,
        IsChooseRoom: false,
        submiting: false,
        radio: '2',
        showPopover: false,
        chosenTypeName: '',
        chosenTypeId: 0,
        totalCostPlaceHolder: '请输入',
        showTime:false,
        minDate:new Date(),
        defaultTime:'',
    },
    computed: {
        formChargeId() {
            return this.form.chargeId;
        },
        formRoomId() {
            return this.form.roomId;
        },
    },
    watch: {
        formUnitPrice(val) {
            this.calculateCost();
        },
        formUseCount(val) {
            this.calculateCost();
        },
        formStartTime(val) {
            this.calculateCost();
        },
        formEndTime(val) {
            this.calculateCost();
        }
    },
    methods: {
        get_data: function () {
            var that = this;
            that.allroomList = [];
            that.roomList = [];
            ns.post({
                action: 'getcyclefeestandardlist',
                roomId: that.RoomID,
            }, function (succeed, data, err) {
                if (succeed) {
                    that.allroomList = data.roomList;
                    that.form.feeTypeList = data.list
                    data.roomList.forEach(function (item) {
                        if (that.RoomIdList.indexOf(item.id) == -1) {
                            return true;
                        }
                        that.roomList.push(item);
                        if (item.id == that.RoomID) {
                            item.isSelected = true;
                            that.form.roomId = item.id;
                            that.form.fullName = item.name;
                            return false;
                        }
                    })
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },

        onShowPopover: function () {
            this.showPopover = !this.showPopover;
        },
        do_choose_room: function () {
            var that = this;
            that.IsMaskOpen = true;
            that.IsChooseRoom = true;
        },

        doSelectRoom: function (item) {
            var that = this;
            that.roomList.forEach(function (item) {
                item.isSelected = false;
            })
            item.isSelected = true;
            that.form.roomId = item.id;
            that.form.fullName = item.name;
            that.RoomID = item.id;
            that.doCloseMask();
            that.get_data();
        },
        do_select_time: function () {
            var that = this;
            that.showTime=true;
            // api.openPicker({
            //     type: 'date',
            //     date: that.form.feeDate,
            //     title: '选择日期'
            // }, function (ret, err) {
            //     if (ret) {
            //         var year = ret.year;
            //         var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
            //         var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
            //         var currentDate = year + '-' + month + '-' + day;
            //         that.form.feeDate = currentDate;
            //     }
            // });
        },
        sureTime:function(value){
            var that=this;
            that.form.feeDate=moment(value).format('YYYY-MM');
            that.showTime=false;

        },
        closeTime:function(){
            var that=this;
            that.showTime=false;
        },
        do_chooseitem: function (item) {
            var that = this;
            item.isSelect = !item.isSelect;

        },
        doCloseMask: function () {
            var that = this;
            that.IsMaskOpen = false;
            that.IsChooseRoom = false;
        },
        do_save: function (item) {
            var that = this;
            if (!that.form.roomId) {
                api.toast({
                    msg: '请选择资源位置',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var typeIdList = []
            that.form.feeTypeList.forEach((typeItem) => {
                if (typeItem.isSelect) {
                    typeIdList.push(typeItem.id);
                }
            })
            if (typeIdList.length == 0) {
                api.toast({
                    msg: '请选择收费标准',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            that.submiting = true;
            ns.post({
                action: 'savecyclefeebystandardlist',
                roomId: that.RoomID,
                ruleIds: JSON.stringify(typeIdList),
                createType: that.radio,
                feeDate: that.form.feeDate
            }, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '保存成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'saveroomfeetempaddfield'
                    });
                    setTimeout(() => {
                        api.closeWin();
                    }, 1000);
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.RoomID = api.pageParam.id || '';
    app.RoomIdList = eval('(' + api.pageParam.roomIds + ')');
    app.get_data();
};
